package com.yycwxt.modules.admin.sale;

import com.yycwxt.common.ReturnResult;
import com.yycwxt.domain.Sale;
import com.yycwxt.modules.admin.store.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@Validated
@Api
@RequestMapping("/sales")
public class SaleController {

    private SaleService saleService;

    @Autowired
    public SaleController(SaleService saleService) {
        this.saleService = saleService;
    }

//    @GetMapping()
//    @ApiOperation("查询全部销售统计")
//    public ReturnResult selectAll(){
//        return ReturnResult.buildSuccessResult(saleService.list());
//    }

   /* @GetMapping("/{id}")
    @ApiOperation("根据ID查询销售统计")
    public ReturnResult selectById(@PathVariable Integer id){
        return ReturnResult.buildSuccessResult(saleService.getById(id));
    }*/

   /* @GetMapping("/getbyzq")
    @ApiOperation("根据账期查询销售统计")
    public ReturnResult getByZq(String zq){
        Map<String, Object> map = new HashMap<>();
        map.put("zq",zq);
        return ReturnResult.buildSuccessResult(saleService.listByMap(map));
    }*/

    @GetMapping()
    @ApiOperation("/获取流水表全部账期的销售统计")
    public ReturnResult getall(){
        return ReturnResult.buildSuccessResult(saleService.getall());
    }

    @GetMapping("/data")
    @ApiOperation("获取数据")
    public ReturnResult getdata(@ApiParam(name = "zq",required = true,type = "int") int zq){
        return ReturnResult.buildSuccessResult(saleService.getsale(zq));
    }

    @PostMapping()
    @ApiOperation("添加销售统计")
    public ReturnResult insertSale(@ApiParam(name = "sale对象",required = true,type = "sale") @RequestBody @Validated Sale sale){
        try {
            return ReturnResult.buildSuccessResult(saleService.save(sale));
        } catch (DuplicateKeyException dke) {
            return ReturnResult.buildFailureResult("id 值重复");
        } catch (Exception e) {
            return ReturnResult.buildFailureResult("参数错误");
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation("根据ID删除销售统计")
    public ReturnResult deleteSale(@PathVariable Integer id){
        return ReturnResult.buildSuccessResult(saleService.removeById(id));
    }

    @PutMapping()
    @ApiOperation("根据id更改销售统计")
    public ReturnResult updateSale(@ApiParam(name = "sale对象",required = true,type = "sale") @RequestBody @Validated Sale sale){
        return ReturnResult.buildSuccessResult(saleService.updateById(sale));
    }

    @PostMapping("/upload")
    @ApiOperation("Excel导入数据")
    public ReturnResult excelToDatabase(@ApiParam(value = "是否删除全部历史记录", type = "boolean") @RequestParam boolean deleteHistory,
                                  @ApiParam(value = "上传文件", type = "_file") @RequestPart("file") MultipartFile file) {
        Result result = new Result();
        if(saleService.importFileToDB(deleteHistory, file)){
           return ReturnResult.buildSuccessResult("添加成功",null);
        }else {
            return ReturnResult.buildFailureResult("添加失败",null);
        }
    }

//    @GetMapping("/zq")
//    @ApiOperation("账期下拉列表")
//    public ReturnResult getzq(){
//        return ReturnResult.buildSuccessResult(saleService.getzq());
//    }

}
